package cn.schoolwow.quickdao.module.database.ddl.query.flow.index.getSingleEntityIndexFlow;

import cn.schoolwow.quickdao.annotation.IndexType;
import cn.schoolwow.quickdao.domain.database.ddl.IndexField;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.Arrays;

public class GetSingleEntityIndexByOracleFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String tableName = (String) flowContext.checkData("tableName");
        String indexName = (String) flowContext.checkData("indexName");
        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("sql", "select table_name, index_name,uniqueness from user_indexes where table_name = ? and index_name = ?")
                .putTemporaryData("parameters", Arrays.asList(tableName,indexName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            IndexField indexField = new IndexField();
                            indexField.tableName = resultSet.getString("table_name");
                            if("UNIQUE".equalsIgnoreCase(resultSet.getString("uniqueness"))){
                                indexField.indexType = IndexType.UNIQUE;
                            }else{
                                indexField.indexType = IndexType.NORMAL;
                            }
                            indexField.indexName = resultSet.getString("index_name");
                            flowContext.putData("indexField", indexField);
                        }
                    }
                })
                .execute();

        IndexField indexField = (IndexField) flowContext.getData("indexField");
        if(null==indexField){
            return;
        }

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取索引字段信息")
                .putTemporaryData("sql", "select table_name,index_name,column_name from user_ind_columns where table_name = ? and index_name = ?")
                .putTemporaryData("parameters", Arrays.asList(tableName, indexName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        while (resultSet.next()) {
                            String columnName = resultSet.getString("column_name");
                            indexField.columns.add(columnName);
                        }
                    }
                })
                .execute();
    }

    @Override
    public String name() {
        return "获取数据库单个表的索引信息-Oracle";
    }
}
